package com.hxht.cmp.controller;

import com.hxht.cmp.common.Constants;
import com.hxht.cmp.common.enums.ResultCode;
import com.hxht.cmp.common.result.ResultResponse;
import com.hxht.cmp.entity.Court;
import com.hxht.cmp.utils.http.HttpClientFormRequest;
import com.hxht.cmp.utils.http.HttpClientResponse;
import com.hxht.cmp.utils.http.HttpClientUtil;
import org.apache.commons.lang3.StringUtils;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

/**
 * 服务器状态
 */
@RestController
@RequestMapping("/server")
public class ServerController {


    /**
     * 获取设备cpu使用率/cpu频率
     *
     * @return
     */
    @RequestMapping("/updateService")
    public ResultResponse updateService(String courtUid) {
        if (StringUtils.isBlank(courtUid)) {
            return ResultResponse.failed(ResultCode.PARAMETER_NULL);
        }
        try {
            Court court = Constants.COURT_MAP.get(courtUid);
            if (null == court) {
                return ResultResponse.failed();
            }
            String serverIp = court.getServiceIp();
            String serverPort = Constants.SERVER_PORT;
            if (StringUtils.isBlank(serverIp) || StringUtils.isBlank(serverPort)) {
                return ResultResponse.failed(ResultCode.SEARCH_NULL);
            }
            String url = "http://" + serverIp + ":" + serverPort + "/updateService";
            HttpClientFormRequest httpClientRequest = new HttpClientFormRequest(url);
            HttpClientResponse response = HttpClientUtil.post(httpClientRequest);
            if (null != response && response.getCode() == 200) {
                return ResultResponse.success(response.getResponseText());
            } else {
                return ResultResponse.failed();
            }
        } catch (Exception e) {
            return ResultResponse.failed();
        }
    }

    /**
     * 获取设备内存使用率、内存空闲情况、内存使用情况(GB)
     *
     * @param courtUid
     * @return
     */
    @RequestMapping("/showServerStatus")
    public ResultResponse showServerStatus(String courtUid) {
        if (StringUtils.isBlank(courtUid)) {
            return ResultResponse.failed(ResultCode.PARAMETER_NULL);
        }
        try {
            Court court = Constants.COURT_MAP.get(courtUid);
            if (null == court) {
                return ResultResponse.failed();
            }
            String serverIp = court.getServiceIp();
            String serverPort = Constants.SERVER_PORT;
            if (StringUtils.isBlank(serverIp) || StringUtils.isBlank(serverPort)) {
                return ResultResponse.failed(ResultCode.SEARCH_NULL);
            }
            String url = "http://" + serverIp + ":" + serverPort + "/showServerStatus";
            HttpClientFormRequest httpClientRequest = new HttpClientFormRequest(url);
            HttpClientResponse response = HttpClientUtil.post(httpClientRequest);
            if (null != response && response.getCode() == 200) {
                return ResultResponse.success(response.getResponseText());
            } else {
                return ResultResponse.failed();
            }
        } catch (Exception e) {
            return ResultResponse.failed();
        }
    }

    /**
     * 获取设备磁盘空间信息
     *
     * @param courtUid
     * @return
     */
    @RequestMapping("/diskStatus")
    public ResultResponse selectDiskStatus(String courtUid) {
        if (StringUtils.isBlank(courtUid)) {
            return ResultResponse.failed(ResultCode.PARAMETER_NULL);
        }
        try {
            Court court = Constants.COURT_MAP.get(courtUid);
            if (null == court) {
                return ResultResponse.failed();
            }
            String serverIp = court.getServiceIp();
            String serverPort = Constants.SERVER_PORT;
            if (StringUtils.isBlank(serverIp) || StringUtils.isBlank(serverPort)) {
                return ResultResponse.failed(ResultCode.SEARCH_NULL);
            }
            String url = "http://" + serverIp + ":" + serverPort + "/diskStatus";
            HttpClientFormRequest httpClientRequest = new HttpClientFormRequest(url);
            HttpClientResponse response = HttpClientUtil.post(httpClientRequest);
            if (null != response && response.getCode() == 200) {
                return ResultResponse.success(response.getResponseText());
            } else {
                return ResultResponse.failed();
            }
        } catch (Exception e) {
            return ResultResponse.failed(ResultCode.SERVER_ERROR);
        }
    }


}
